home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / AETSK102.ZIP / contrib / tasks / test / test.cc < prev   
C/C++ Source or Header  |  1993-12-02  |  4KB  |  170 lines

  1. /**********************************************************************
  2.  *  
  3.  *  NAME:           test.cpp
  4.  *  
  5.  *  DESCRIPTION:    test program for Task++
  6.  *  
  7.  *  copyright (c) 1991 J. Alan Eldridge
  8.  * 
  9.  *  M O D I F I C A T I O N   H I S T O R Y
  10.  *
  11.  *  when        who                 what
  12.  *  -------------------------------------------------------------------
  13.  *  May 91      J. Alan Eldridge    created
  14.  *  
  15.  *  10/11/91    JAE                 modified class Killer to use the
  16.  *                                  keyboard reading task
  17.  *
  18.  *********************************************************************/
  19.  
  20. #include    "aedef.h"
  21. #include    "task.h"
  22.  
  23. #include    "keybdtsk.h"
  24.  
  25. class Killer: public Task {
  26. public:
  27.     Killer(char *name): Task(name)
  28.         { }
  29.     void    TaskMain();
  30. };
  31.  
  32. void
  33. Killer::TaskMain()
  34. {
  35.     uchar   ch;
  36.     
  37.     for (;;) {
  38.         ch = '\r';
  39.         KbdPipe.recv(ch, 15000);
  40.         if (ch == 0 || ch == 0xE0) {
  41.             KbdPipe >> ch;
  42.             scheduler(0);
  43.         } else {
  44.             TaskKill(&KbdTask);
  45.             break;
  46.         }
  47.     }
  48.     printf("task %s returning\n", name());
  49. }
  50.  
  51. class Sender: public Task {
  52. public:
  53.     Sender(char *name): Task(name)
  54.         { }
  55.     void    TaskMain();
  56. };
  57.  
  58. class Receiver: public Task {
  59. public:
  60.     Receiver(char *name): Task(name)
  61.         { }
  62.     void    TaskMain();
  63. };
  64.  
  65. class Numbers: virtual public ChildTask {
  66. private:
  67.     int cnt;
  68. public:
  69.     Numbers(char *name, int n): ChildTask(name), cnt(n) 
  70.         { }
  71.     virtual void    TaskMain();
  72. };
  73.  
  74. //----------------------------------------------------------------------
  75. //
  76. //  a word about this structure, its constructors, and member functions,
  77. //  and the problems inherent in trying to queue something that has an
  78. //  assignment operator (and a copy constructor, presumably) and a destructor:
  79. //
  80. //  To be placed on a CPipe, an object should have defined:
  81. //
  82. //  1. a default constructor with no args
  83. //  2. an assignment operator taking an argument of type "type&"
  84. //  3. an explicit destructor
  85. //
  86. //  When receiving from the CPipe, you'll need a variable of the
  87. //  appropriate type... it will have been initialized by the default
  88. //  constructor. The receive function will destruct it before doing
  89. //  the assignment. If you timeout on a receive, it will not have
  90. //  been destroyed, so that we keep an even balance of constructors
  91. //  (assignments) and destructors. (I know this is a little complex,
  92. //  but I had to figure it out by experiment myself... it's not that
  93. //  easy to figure out what calls the compiler is going to generate.)
  94. //
  95.  
  96. struct TN {
  97.     char   name[ 32 ];
  98.     TN() { printf("TN:default constructor\n"); name[0] = 0; }
  99.     TN(char *s) { printf("TN:char* constructor\n"); strcpy(name, s); }
  100.     ~TN() { printf("TN:destroying\n"); name[0] = 0; }
  101.     TN(TN &tn) { printf("TN:calling copy\n"); strcpy(name, tn.name); }
  102.     TN &operator=(TN &tn) { printf("TN:calling assign\n"); 
  103.         strcpy(name, tn.name); return *this; }
  104. };
  105.  
  106. declare(CPipe_,TN);
  107. implement(CPipe_,TN);
  108.  
  109. typedef CPipe(TN)   TNPipe;
  110.  
  111. TNPipe  tnp(10);
  112.  
  113. void
  114. Sender::TaskMain()
  115. {
  116.     TN  tn(name());
  117.     
  118.     for (int n = 0; n < 20; n++) {
  119.         printf("task %s sending (%d)\n", name(), n);
  120.         tnp << tn;
  121.         printf("task %s returned (%d)\n", name(), n);
  122.     }
  123.  
  124.     printf("task %s spawning Numbers task\n", name());
  125.  
  126.     Numbers *pn = new Numbers(name(), 5);
  127.     pn->wait();
  128.     delete pn;
  129.     
  130.     printf("task %s returning\n", name());
  131. }
  132.  
  133. void
  134. Receiver::TaskMain()
  135. {
  136.     TN  tn;
  137.     
  138.     priority(2);
  139.  
  140.     for (;;) {
  141.         printf("task %s receiving\n", name());
  142.         if (tnp.recv(tn, 2000) < 1)
  143.             break;
  144.         printf("task %s received <%s>\n", name(), tn.name);
  145.     }
  146.     printf("task %s returning\n", name());
  147. }
  148.  
  149. void
  150. Numbers::TaskMain()
  151. {
  152.     for (int i = 0; i < cnt; i++) {
  153.         printf("Numbers %2d\n", i);
  154.         suspend();
  155.     }
  156. }
  157.  
  158. Killer      k("Killer");
  159.  
  160. Sender      s2("Sender#2");
  161. Sender      s1("Sender#1");
  162.  
  163. Receiver    r1("Receiver#1");
  164.  
  165. main()
  166. {
  167.     printf("scheduler() returned %d\n", scheduler());
  168.     return 0;
  169. }
  170.